import pygame, sys, math, time

class Player(pygame.sprite.Sprite):
	def __init__(self, pos = (0,0), blocksize = [50,50], screensize = [1000,600]):
		pygame.sprite.Sprite.__init__(self, self.containers)
		self.screensize = screensize
		self.stillRightImages = [pygame.image.load("rsc/player/stickStillRight.png"),
								 pygame.image.load("rsc/player/stickStillRight.png"),
								 pygame.image.load("rsc/player/stickStillRight.png"),
								 pygame.image.load("rsc/player/stickStillRight.png")]
		
		self.stillLeftImages = [pygame.image.load("rsc/player/stickStillLeft.png"),
								pygame.image.load("rsc/player/stickStillLeft.png"),
								pygame.image.load("rsc/player/stickStillLeft.png"),
								pygame.image.load("rsc/player/stickStillLeft.png")]
		
		self.jumpRightImages = [pygame.image.load("rsc/player/stickRunRight3.png"),
								pygame.image.load("rsc/player/stickRunRight3.png"),
								pygame.image.load("rsc/player/stickRunRight3.png"),
								pygame.image.load("rsc/player/stickRunRight3.png")]
		
		self.jumpLeftImages = [pygame.image.load("rsc/player/stickRunLeft3.png"),
							   pygame.image.load("rsc/player/stickRunLeft3.png"),
							   pygame.image.load("rsc/player/stickRunLeft3.png"),
							   pygame.image.load("rsc/player/stickRunLeft3.png")]
		
		self.crouchRightImages = [pygame.image.load("rsc/player/stickCrouchRight.png"),
								  pygame.image.load("rsc/player/stickCrouchRight.png"),
								  pygame.image.load("rsc/player/stickCrouchRight.png"),
								  pygame.image.load("rsc/player/stickCrouchRight.png")]
		
		self.crouchLeftImages = [pygame.image.load("rsc/player/stickCrouchLeft.png"),
								 pygame.image.load("rsc/player/stickCrouchLeft.png"),
								 pygame.image.load("rsc/player/stickCrouchLeft.png"),
								 pygame.image.load("rsc/player/stickCrouchLeft.png")]

		self.runRightImages = [pygame.image.load("rsc/player/stickRunRight1.png"),
							   pygame.image.load("rsc/player/stickRunRight2.png"),
							   pygame.image.load("rsc/player/stickRunRight3.png"),
							   pygame.image.load("rsc/player/stickRunRight2.png")]
		
		self.runLeftImages = [pygame.image.load("rsc/player/stickRunLeft1.png"),
							  pygame.image.load("rsc/player/stickRunLeft2.png"),
							  pygame.image.load("rsc/player/stickRunLeft3.png"),
							  pygame.image.load("rsc/player/stickRunLeft2.png")]				  
		
		self.images = self.stillRightImages
		self.frame = 0
		self.maxFrame = len(self.images) - 1
		self.waitCount = 0
		self.waitCountMax = 5
		self.image = self.images[self.frame]
		self.rect = self.image.get_rect()
		self.maxSpeed = blocksize[0]/10.0
		self.speed = [0,0]
		self.speedx = 0
		self.speedy = 0
		self.g = blocksize[0]/10
		self.jumpSpeed = 0
		self.jumpSpeedMax = 50
		self.fallSpeedMax = int(blocksize[0]/2) -2
		self.realx = pos[0]
		self.realy = pos[1]
		self.x = screensize[0]/2
		self.y = screensize[1]/2
		self.offsetx = self.x - self.realx
		self.offsety = self.y - self.realy
		self.scrollingx = True
		self.scrollingy = True
		self.scrollBoundary = 100
		self.headingx = "right"
		self.headingy = "up"
		self.facing = "right"
		self.jumping = False
		self.headingChanged = False
		self.radius = self.rect.width/2
		self.living = True
		self.place(pos)
		#self.onfloor = False
		self.floor = screensize[1]
		self.touchFloor = False		
		
	def place(self, pos):
		self.rect.center = pos
		
	def fixLocation(self, x, y):
		pass
		
	def update(*args):
		self = args[0]
		self.collideWall(self.screensize)
		if (self.rect.bottom < self.floor) and self.headingy == "none":
			self.headingy = "down"
		#if self.rect.bottom == self.floor:
		#	 self.touchFloor = True
		self.animate()
		self.move()
		self.touchFloor = False
		#
		#print self.speedy
	
	def animate(self):
		self.image = self.images[self.frame]
		self.rect = self.image.get_rect(center = self.rect.center)
		self.maxFrame = len(self.images) - 1
		
		if self.waitCount < self.waitCountMax:
			self.waitCount += 1
		else:
			self.waitCount = 0
			if self.frame < self.maxFrame:
				self.frame += 1
			else:
				self.frame = 0
			self.image = self.images[self.frame]
			self.rect = self.image.get_rect(center = self.rect.center)		  
		
	def move(self):
	
		if not self.touchFloor:
			self.headingy = "down"
		if self.headingy == "down":
			if self.speedy < self.fallSpeedMax:
				self.speedy += self.g
			else:
				self.speedy = self.fallSpeedMax
				
		self.realx += self.speedx
		self.realy += self.speedy

		"""
		if not self.scrollingx:
			self.x += self.speedx
		if self.x > self.screensize[0] - self.scrollBoundary and self.headingx == "right":
			self.scrollingx = True
		elif self.x < self.scrollBoundary and self.headingx == "left":
			self.scrollingx = True
		else:
			self.scrollingx = False
			
			
		if not self.scrollingy:
			self.y += self.speedy
		if self.y > self.screensize[1] - self.scrollBoundary:
			self.scrollingy = True
			print "scroll down"
		elif self.y < self.scrollBoundary:
			self.scrollingy = True
			print "scroll up"
		else:
			self.scrollingy = False
			print "no scroll"
		"""	   
		self.rect.center = (round(self.x), round(self.y))
		
	def collideWall(self, size):
		if self.rect.left < 0 and self.headingx == "left":
			self.speedx = 0
		elif self.rect.right > size[0] and self.headingx == "right":
			self.speedx = 0
		if self.rect.top < 0 and self.headingy == "up":
			self.speedy = 0
		elif self.rect.bottom > size[1] and self.headingy == "down":
			self.speedy = 0
	
	def collideBlock(self, block):
		#time.sleep(.25)
		#print self.rect, self.headingx, self.headingy
		if self.floor == block.rect.top + 2 and self.headingy == "none":
			self.touchFloor = True
			self.jumping = False
		else:
			#print self.realx, block.realx, self.headingxa
			if self.realy < block.rect.bottom + self.rect.height/2:
				self.speedy = -self.speedy
				#self.y += 1
				print "hit up"
				
			#if self.realy > block.realy - (25 + self.rect.height/2) and self.headingy == "down":
			if self.realy > block.realy - (25 + self.rect.height/2):
				#self.speedy = height
				#self.realy -= 1
				#self.y -= 1
				
				self.speedy = 0
				
				#self.realy -= self.g
				#self.y -= self.g + 2
				#self.realy = block.rect.top - self.rect.height/2 + 2
				#self.realy = block.rect.top - self.rect.height/2 + 2
				#print newy, self.y, self.realy, self.realy + newy - self.y
				#time.sleep(.5)
				 
				#self.y = newy
				
				
				self.headingy = "none"
				self.floor = block.rect.top+2
				self.touchFloor = True
				self.realy += block.realy - self.realy + 2
				#print "hit down" 
				return
			
			if self.realx < block.realx and self.headingx == "right":
				self.speedx = 0
				self.realx -= 1
				#self.x -= 1
				print "hit right"
			if self.realx > block.realx and self.headingx == "left":
				self.speedx = 0
				self.realx += 1
				#self.x += 1
				print "hit left"
			if self.realy < block.rect.top + self.rect.height/2:
				self.speedy = -self.speedy
				#self.y += 1
				print "hit up"
			
   
	def direction(self, dir):
		if dir == "right":
			self.headingx = "right"
			self.speedx = self.maxSpeed
			self.facing = "right"
			self.images = self.runRightImages
		if dir == "stop right":
			self.headingx = "right"
			self.speedx = 0
			self.images = self.stillRightImages
		if dir == "left":
			self.headingx = "left"
			self.speedx = -self.maxSpeed
			self.facing = "left"
			self.images = self.runLeftImages
		if dir == "stop left":
			self.headingx = "left"
			self.speedx = 0
			self.images = self.stillLeftImages
		if dir == "stop up":
			self.headingy = "up"
			self.speedy = 0
			if self.facing == "right":
				self.images = self.jumpRightImages
			else:
				self.images = self.jumpLeftImages
		if dir == "down":
			self.headingy = "down"
			if self.facing == "right":
				self.images = self.crouchRightImages
			else:
				self.images = self.crouchLeftImages
		if dir == "stop down":
			self.headingy = "down"
			self.speedy = 0
			if self.facing == "right":
				self.images = self.stillRightImages
			else:
				self.images = self.stillLeftImages
		
	def jump(self):
		 if not self.jumping:
			self.speedy = -30
			#self.headingy = "up"
			self.jumping = True
			self.touchFloor = False

		
	
